#include "searchlineedit.h"
#include "util.h"

#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>

SearchLineEdit::SearchLineEdit(QWidget *parent)
    : QLineEdit{parent}
{
    this->setPlaceholderText("请搜索你感兴趣的视频");
    this->setFixedSize(320,36);
    this->setObjectName("search");
    this->setStyleSheet("#search{"
                        "background-color: #FFFFFF;"
                        "border-radius: 10px;"
                        "border: 1px solid #3ECEFE;"
                        "font-family: 微软雅黑;"
                        "font-size: 14px;"
                        "font-style: normal;"
                        "}");
    this->setTextMargins(33,0,0,0);

    // 搜索框图标
    QLabel* searchImg = new QLabel(this);
    searchImg->setFixedSize(16,16);
    searchImg->setPixmap(QPixmap(":/images/homePage/sousuo.png"));

    // 搜索按钮
    QPushButton* searchBtn = new QPushButton(this);
    searchBtn->setText("搜索");
    searchBtn->setFixedSize(62,32);
    searchBtn->setCursor(QCursor(Qt::ArrowCursor));
    searchBtn->setStyleSheet("background-color: #3ECEFE;"
                             "border-radius: 16px;"
                             "font-family: 微软雅黑;"
                             "font-size: 14px;"
                             "color: #FFFFFF;"
                             "font-style: normal;");

    // 设置布局管理器
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(11,0,2,0);
    layout->addWidget(searchImg);
    layout->addStretch();
    layout->addWidget(searchBtn);
    this->setLayout(layout);

    connect(this, &QLineEdit::returnPressed, this, &SearchLineEdit::onSearchBtnClicked);
    connect(searchBtn, &QPushButton::clicked, this, &SearchLineEdit::onSearchBtnClicked);
}

void SearchLineEdit::onSearchBtnClicked()
{
    emit searchVideos(this->text());
}
